// pages/customer-detail/customer-detail.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_sj: {},
    /*  */
    hidePage:true,
    isAdd:false,
    type:'',
    tabType:'',
    typeName:'',
    submitArgs:{
      id:'',
      //name:'',   //姓名
      insuredName:'', //客户姓名
      //mobile:'',  //电话
      insuredMobile:'', //客户电话
      licenseNo:'',  //车牌
      moldName:'',    //车辆品牌
      remark:'',
      storeId:'',  // 店铺id
      storeName:''  //
    },
    /*  */
    submitBtnData:[
      {
        name:'保存'
      }
    ],
    /*  */
    detailArgs: {
      type:'',
      id:''
    }
  },
  /*  */
  backToCustomerList: function(){
    wx.navigateTo({
      url: '/pages/customer/customer',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_sj: app.globalData.get_userInfo()
    })
    //console.log(this.data.userInfo_sj)
    this.setData({
      'submitArgs.storeId': this.data.userInfo_sj.storeId
    })
    /*  */
    let eventChannel = this.getOpenerEventChannel()
    eventChannel.on('add-customer',res=>{
      console.log(res)
      let { type } = res
      if(type === 'add'){
        wx.setNavigationBarTitle({
          title: '添加客户-'+res.typeName,
        })
        this.setData({
          tabType:res.tabType,
          typeName:res.typeName,
          hidePage:false,
          type:type,
          isAdd:true
        })
      }
      if(type === 'detail0'){
        wx.setNavigationBarTitle({
          title: '客户详情-'+res.typeName,
        })
        this.setData({
          tabType:res.tabType,
          hidePage:false,
          type:type,
          isAdd:false,
          'detailArgs.type':res.tabType,
          'detailArgs.id':res.id
        })
        wx.nextTick(()=>{
          console.log(this.data.detailArgs)
          this.getClientInfo()
        })
      }
      if(type === 'detail1'){
        wx.setNavigationBarTitle({
          title: '客户详情-'+res.typeName,
        })
        this.setData({
          tabType:res.tabType,
          hidePage:false,
          type:type,
          isAdd:false,
          'detailArgs.type':res.tabType,
          'detailArgs.id':res.id
        })
        wx.nextTick(()=>{
          console.log(this.data.detailArgs)
          this.getClientInfo()
        })
      }
    })
  },
  /* 拨打客户电话 */
  callCustomer: function(e){
    if(!this.data.isAdd&&this.data.type=='detail0'){
      let { info }  = e.currentTarget.dataset
      wx.makePhoneCall({
        phoneNumber:''+info.mobile,
        success: ()=>{},
        fail: ()=>{}
      })
    }
  },
  /* 编辑客户 */
  updateStoreClient: function(){
    if(!this.data.submitArgs.insuredName){
      wx.showToast({
        icon:'none',
        title:'请输入客户姓名'
      })
      return
    }
    let regx = /^1\d{10}$/g
    if(!regx.test(this.data.submitArgs.insuredMobile)){
      wx.showToast({
        icon:'none',
        title:'请输入正确的客户手机号码'
      })
      return
    }
    wx.showLoading({
      title: '修改客户信息',
      mask:true
    })
    this.setData({
      'submitArgs.storeId':this.data.userInfo_sj.storeId,
      'submitArgs.storeName':this.data.userInfo_sj.storeName
    })
    wx.request({
      url: app.globalData.Url + '/wx/updateStoreClient',
      method: 'POST',
      header: this.data.userInfo_sj.header,
      data: util.json2Form({
        client:JSON.stringify(this.data.submitArgs),
        id:this.data.submitArgs.id
      }),   //
      success: res => {
        console.log(res)
        //token失效
        if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
        if (res.data.code == '1') {
          wx.showToast({
            title: ''+res.data.msg
          })
          //
          wx.navigateTo({
            url:'/pages/customer/customer'
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: ''+res.data.msg
          })
        }
      },
      fail: err => {
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: '修改客户信息失败'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  /* 获取客户详情 */
  getClientInfo: function(){
    wx.showLoading({
      title: '获取客户详情',
      mask:true
    })
    return new Promise((resolve, reject)=>{
      wx.request({
        url: app.globalData.Url + '/wx/getClientInfo',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form(this.data.detailArgs),   //
        success: res => {
          console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1') {
            this.setData({
              'submitArgs.id':res.data.body.id||'',
              'submitArgs.insuredName':res.data.body.insuredName,
              'submitArgs.insuredMobile':res.data.body.insuredMobile,
              'submitArgs.licenseNo':res.data.body.licenseNo,
              'submitArgs.moldName':res.data.body.moldName,
              'submitArgs.remark':res.data.body.remark?res.data.body.remark:'-'
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: ''+res.data.msg
            })
          }
        },
        fail: err => {
          console.log(err)
          wx.showToast({
            icon: 'none',
            title: '获取客户详情失败'
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  /* 删除客户 */
  deleteStoreClient: function(){
    if(this.data.detailArgs.type=='0'){
      wx.showToast({
        icon:'none',
        title:'保险客户不能删除'
      })
      return
    }
    wx.showModal({
      title:'提示',
      content:'确定要删除该客户吗？',
      success: res=>{
        if(res.confirm){
          wx.showLoading({
            title: '删除客户',
            mask:true
          })
          wx.request({
            url: app.globalData.Url + '/wx/deleteStoreClient',
            method: 'POST',
            header: this.data.userInfo_sj.header,
            data: util.json2Form({
              id:this.data.detailArgs.id
            }),   //
            success: res => {
              console.log(res)
              //token失效
              if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
              if (res.data.code == '1') {
                wx.showToast({
                  title: ''+res.data.msg
                })
                //
                wx.navigateTo({
                  url:'/pages/customer/customer',
                  success: res => {
                    res.eventChannel.emit('back-to-customerList',{
                      deleteCustomer:true
                    })
                  }
                })
              } else {
                wx.showToast({
                  icon: 'none',
                  title: ''+res.data.msg
                })
              }
            },
            fail: err => {
              console.log(err)
              wx.showToast({
                icon: 'none',
                title: '删除客户失败'
              })
            },
            complete: () => {
              wx.hideLoading()
            }
          })
        }
      }
    })
  },
  /* 输入客户姓名 */
  getCustomerName: function(e){
    this.setData({
      //'submitArgs.name':e.detail.value
      'submitArgs.insuredName':e.detail.value
    })
  },
  /* 输入客户电话 */
  getCustomerPhone: function(e){
    this.setData({
      //'submitArgs.mobile':e.detail.value
      'submitArgs.insuredMobile':e.detail.value
    })
  },
  /* 输入客户车牌 */
  getCustomerLicenseNo: function(e){
    this.setData({
      'submitArgs.licenseNo':e.detail.value
    })
  },
  /* 输入客户车辆品牌 */
  getCustomerMoldname: function(e){
    this.setData({
      'submitArgs.moldName':e.detail.value
    })
  },
  /*  */
  getCustomerRemark: function(e){
    this.setData({
      'submitArgs.remark':e.detail.value
    })
  },
  /* 新增提交 */
  submitCustomer: function(e){
    if(!this.data.submitArgs.insuredName){
      wx.showToast({
        icon:'none',
        title:'请输入客户姓名'
      })
      return
    }
    let regx = /^1\d{10}$/g
    if(!regx.test(this.data.submitArgs.insuredMobile)){
      wx.showToast({
        icon:'none',
        title:'请输入正确的客户手机号码'
      })
      return
    }
    wx.showLoading({
      title: '添加客户',
      mask:true
    })
    console.log(this.data.submitArgs)
    this.setData({
      'submitArgs.storeId':this.data.userInfo_sj.storeId,
      'submitArgs.storeName':this.data.userInfo_sj.storeName
    })
    wx.request({
      url: app.globalData.Url + '/wx/insertStoreClient',
      method: 'POST',
      header: this.data.userInfo_sj.header,
      data: util.json2Form(this.data.submitArgs),   //
      success: res => {
        console.log(res)
        //token失效
        if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
        if (res.data.code == '1') {
          wx.showToast({
            title: ''+res.data.msg
          })
          //
          wx.navigateTo({
            url:'/pages/customer/customer'
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: ''+res.data.msg
          })
        }
      },
      fail: err => {
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: '添加客户失败'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /*  */
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})